package com.explorati.LeetCode215.Kthlargestelementinanarray;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Solution2 {
	public static int findKthLargest(int[] nums, int k) {
		PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			}
		});
		for (int i = 0; i < nums.length; i++) {
			pq.add(nums[i]);
		}
		while (pq.size() > nums.length - k + 1) {
			pq.poll();
		}
		return pq.peek();
	}

	public static void main(String[] args) {
		int[] arr = { 3, 2, 1, 5, 6, 4 };
		int i = findKthLargest(arr, 2);
		System.out.println(i);
	}
}
